package com.miao.elm.controller;

import com.miao.elm.entity.CartEntity;
import com.miao.elm.service.CartService;
import com.miao.elm.to.CartTo;
import com.miao.elm.to.FoodTo;
import com.miao.elm.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("CartController")
public class CartController {
    @Autowired
    private CartService cartService;

    @RequestMapping("/listCart")
    public List<FoodTo> listCart(CartTo cartTo){
        System.out.println("查询购物车");
        return cartService.listCartByInfo(cartTo);
    }

    @PostMapping("/saveCart")
    public int saveCart(CartTo cartTo){
        return cartService.saveCart(cartTo);
    }

    @PostMapping("/updateCart")
    public int updateCart(CartTo cartTo){
        return cartService.updateCart(cartTo);
    }

    @PostMapping("/removeCart")
    public int removeCart(CartTo cartTo){
        return cartService.removeCart(cartTo);
    }

    @RequestMapping("/info/{cartId}")
    public R info(@PathVariable("cartId") Integer cartId){
		CartEntity cart = cartService.getById(cartId);

        return R.ok().put("cart", cart);
    }

    @RequestMapping("/save")
    public R save(@RequestBody CartEntity cart){
		cartService.save(cart);

        return R.ok();
    }

    @RequestMapping("/update")
    public R update(@RequestBody CartEntity cart){
		cartService.updateById(cart);

        return R.ok();
    }

    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] cartIds){
		cartService.removeByIds(Arrays.asList(cartIds));

        return R.ok();
    }

}
